package com.zf.yichat.utils.HJpay;

import com.alibaba.fastjson.JSON;
import com.zf.yichat.utils.HJpay.com.sdk.entity.WithdrawResponse;
import com.zf.yichat.utils.HJpay.util.HttpClientUtil;
import com.zf.yichat.utils.HJpay.util.Md5_Sign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 快捷支付-协议支付接口
 *
 * @author Lori
 * @version 2019年11月
 */
public class HjWithdrawUtil {
    private static Logger logger = LoggerFactory.getLogger(HjWithdrawUtil.class);
    public static String getHjResponse(String md5Key, Map<String, Object> map) throws Exception {


        String reqSign = getRequestSign(map);
        logger.error("md5Key--》1---》"+reqSign);
        logger.error("md5Key--》2---》"+md5Key);
        // 签名
        String hmac = Md5_Sign.SignByMD5(reqSign, md5Key);
        map.put("hmac", hmac);

        // Map转json字符串
        String reqBodyJson = JSON.toJSONString(map);
        System.out.println("reqBodyJson：" + reqBodyJson);
        String httpResponseJson = HttpClientUtil
                .sendHttpPost("https://www.joinpay.com/payment/pay/singlePay", reqBodyJson);
        System.out.println("respStr：" + httpResponseJson);

        return httpResponseJson;
//        doResponseInfo(httpResponseJson, md5Key);
    }

    /**
     * 对单笔代付响应信息的处理
     *
     * @param httpResponseJson 响应信息json字符串
     * @param key              商户秘钥
     * @throws Exception 异常实体类
     */
//    @SuppressWarnings("unchecked")
//    private static void doResponseInfo(String httpResponseJson, String key) throws Exception {
//
//        // 响应信息map集合
//        Map<String, Object> httpResponseMap = (Map<String, Object>) JSONObject.parse(httpResponseJson);
//        // 业务数据map集合
//        Map<String, Object> dataMap = (Map<String, Object>) httpResponseMap.get("data");
//        dataMap.put("statusCode", httpResponseMap.get("statusCode"));
//        dataMap.put("message", httpResponseMap.get("message"));
//
//        // 请求签名串
//        String reqSign = getRequestSign(httpResponseMap);
//        // 响应签名串
//        String respSign = getResponseSign(dataMap);
//        System.out.println("respSign：" + respSign);
//        // 请求数据的加密签名
//        String reqHmac = Md5_Sign.SignByMD5(respSign, key);
//        // 请求数据的加密签名
//        String respHmac = (String) dataMap.get("hmac");
//        System.out.println("reqHmac：" + reqHmac);
//        System.out.println("respSign：" + respHmac);
//
//        reqHmac = reqHmac.toUpperCase();
//        respHmac = respHmac.toUpperCase();
//        boolean isMatch = reqHmac.equals(respHmac);
//        if (isMatch) {
//            System.out.println("验签成功");
//        } else {
//            System.out.println("验签失败");
//        }
//    }

    /**
     * 获取请求数据签名串信息
     * 必须按新代付接口文档请求参数信息顺序来进行字符串的拼接，详情请参考新代付接口文档请求报文
     *
     * @param params 请求数据参数
     * @return 返回请求签名串
     */
    public static String getRequestSign(Map<String, Object> params) {

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(params.get("userNo")).append(params.get("productCode")).append(params.get("requestTime"))
                .append(params.get("merchantOrderNo")).append(params.get("receiverAccountNoEnc"))
                .append(params.get("receiverNameEnc")).append(params.get("receiverAccountType"));
        if (params.get("receiverBankChannelNo") != null) {
            stringBuilder.append(params.get("receiverBankChannelNo"));
        }
        stringBuilder.append(params.get("paidAmount"))
                .append(params.get("currency")).append(params.get("isChecked")).append(params.get("paidDesc"))
                .append(params.get("paidUse")).append(params.get("callbackUrl"));
        System.out.println("reqSign：" + stringBuilder);
        return stringBuilder.toString();
    }

    /**
     * 获取响应数据签名串信息
     * 必须按新代付接口文档应答参数信息顺序来进行字符串的拼接，详情请参考新代付接口文档的应答报文
     *
     * @param params 响应数据参数
     * @return 返回响应签名串
     */
    public static String getResponseSign(WithdrawResponse params) {

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(params.getStatus());
        if (params.getErrorCode() != null) {
            stringBuilder.append(params.getErrorCode());
        }
        if (params.getErrorCodeDesc() != null) {
            stringBuilder.append(params.getErrorCodeDesc());
        }
        stringBuilder.append(params.getUserNo());
        if (params.getTradeMerchantNo() != null) {
            stringBuilder.append(params.getTradeMerchantNo());
        }
        stringBuilder.append(params.getMerchantOrderNo())
                .append(params.getPlatformSerialNo()).append(params.getReceiverAccountNoEnc()).append(params.getReceiverNameEnc())
                .append(params.getPaidAmount()).append(params.getFee());

        return stringBuilder.toString();
    }
}
